﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http.Headers;

namespace zijian666.Http;

/// <summary>
/// 表示各种文件后缀名对应 Content-Type 标头的在 RFC 2616 中定义的媒体类型的映射关系
/// </summary>
public static class MediaTypes
{
    private readonly static ICloneable _default = new MediaTypeHeaderValue("application/octet-stream");

    private readonly static Dictionary<string, ICloneable> _mapping = new()
    {
        [".tif"] = new MediaTypeHeaderValue("image/tiff"),
        [".001"] = new MediaTypeHeaderValue("application/x-001"),
        [".301"] = new MediaTypeHeaderValue("application/x-301"),
        [".323"] = new MediaTypeHeaderValue("text/h323"),
        [".906"] = new MediaTypeHeaderValue("application/x-906"),
        [".907"] = new MediaTypeHeaderValue("drawing/907"),
        [".a11"] = new MediaTypeHeaderValue("application/x-a11"),
        [".acp"] = new MediaTypeHeaderValue("audio/x-mei-aac"),
        [".ai"] = new MediaTypeHeaderValue("application/postscript"),
        [".aif"] = new MediaTypeHeaderValue("audio/aiff"),
        [".aifc"] = new MediaTypeHeaderValue("audio/aiff"),
        [".aiff"] = new MediaTypeHeaderValue("audio/aiff"),
        [".anv"] = new MediaTypeHeaderValue("application/x-anv"),
        [".asa"] = new MediaTypeHeaderValue("text/asa"),
        [".asf"] = new MediaTypeHeaderValue("video/x-ms-asf"),
        [".asp"] = new MediaTypeHeaderValue("text/asp"),
        [".asx"] = new MediaTypeHeaderValue("video/x-ms-asf"),
        [".au"] = new MediaTypeHeaderValue("audio/basic"),
        [".avi"] = new MediaTypeHeaderValue("video/avi"),
        [".awf"] = new MediaTypeHeaderValue("application/vnd.adobe.workflow"),
        [".biz"] = new MediaTypeHeaderValue("text/xml"),
        [".bmp"] = new MediaTypeHeaderValue("application/x-bmp"),
        [".bot"] = new MediaTypeHeaderValue("application/x-bot"),
        [".c4t"] = new MediaTypeHeaderValue("application/x-c4t"),
        [".c90"] = new MediaTypeHeaderValue("application/x-c90"),
        [".cal"] = new MediaTypeHeaderValue("application/x-cals"),
        [".cat"] = new MediaTypeHeaderValue("application/vnd.ms-pki.seccat"),
        [".cdf"] = new MediaTypeHeaderValue("application/x-netcdf"),
        [".cdr"] = new MediaTypeHeaderValue("application/x-cdr"),
        [".cel"] = new MediaTypeHeaderValue("application/x-cel"),
        [".cer"] = new MediaTypeHeaderValue("application/x-x509-ca-cert"),
        [".cg4"] = new MediaTypeHeaderValue("application/x-g4"),
        [".cgm"] = new MediaTypeHeaderValue("application/x-cgm"),
        [".cit"] = new MediaTypeHeaderValue("application/x-cit"),
        [".class"] = new MediaTypeHeaderValue("java/*"),
        [".cml"] = new MediaTypeHeaderValue("text/xml"),
        [".cmp"] = new MediaTypeHeaderValue("application/x-cmp"),
        [".cmx"] = new MediaTypeHeaderValue("application/x-cmx"),
        [".cot"] = new MediaTypeHeaderValue("application/x-cot"),
        [".crl"] = new MediaTypeHeaderValue("application/pkix-crl"),
        [".crt"] = new MediaTypeHeaderValue("application/x-x509-ca-cert"),
        [".csi"] = new MediaTypeHeaderValue("application/x-csi"),
        [".css"] = new MediaTypeHeaderValue("text/css"),
        [".cut"] = new MediaTypeHeaderValue("application/x-cut"),
        [".dbf"] = new MediaTypeHeaderValue("application/x-dbf"),
        [".dbm"] = new MediaTypeHeaderValue("application/x-dbm"),
        [".dbx"] = new MediaTypeHeaderValue("application/x-dbx"),
        [".dcd"] = new MediaTypeHeaderValue("text/xml"),
        [".dcx"] = new MediaTypeHeaderValue("application/x-dcx"),
        [".der"] = new MediaTypeHeaderValue("application/x-x509-ca-cert"),
        [".dgn"] = new MediaTypeHeaderValue("application/x-dgn"),
        [".dib"] = new MediaTypeHeaderValue("application/x-dib"),
        [".dll"] = new MediaTypeHeaderValue("application/x-msdownload"),
        [".doc"] = new MediaTypeHeaderValue("application/msword"),
        [".dot"] = new MediaTypeHeaderValue("application/msword"),
        [".drw"] = new MediaTypeHeaderValue("application/x-drw"),
        [".dtd"] = new MediaTypeHeaderValue("text/xml"),
        [".dwf"] = new MediaTypeHeaderValue("Model/vnd.dwf"),
        [".dwg"] = new MediaTypeHeaderValue("application/x-dwg"),
        [".dxb"] = new MediaTypeHeaderValue("application/x-dxb"),
        [".dxf"] = new MediaTypeHeaderValue("application/x-dxf"),
        [".edn"] = new MediaTypeHeaderValue("application/vnd.adobe.edn"),
        [".emf"] = new MediaTypeHeaderValue("application/x-emf"),
        [".eml"] = new MediaTypeHeaderValue("message/rfc822"),
        [".ent"] = new MediaTypeHeaderValue("text/xml"),
        [".epi"] = new MediaTypeHeaderValue("application/x-epi"),
        [".eps"] = new MediaTypeHeaderValue("application/postscript"),
        [".etd"] = new MediaTypeHeaderValue("application/x-ebx"),
        [".exe"] = new MediaTypeHeaderValue("application/x-msdownload"),
        [".fax"] = new MediaTypeHeaderValue("image/fax"),
        [".fdf"] = new MediaTypeHeaderValue("application/vnd.fdf"),
        [".fif"] = new MediaTypeHeaderValue("application/fractals"),
        [".fo"] = new MediaTypeHeaderValue("text/xml"),
        [".frm"] = new MediaTypeHeaderValue("application/x-frm"),
        [".g4"] = new MediaTypeHeaderValue("application/x-g4"),
        [".gbr"] = new MediaTypeHeaderValue("application/x-gbr"),
        ["."] = new MediaTypeHeaderValue("application/x-"),
        [".gif"] = new MediaTypeHeaderValue("image/gif"),
        [".gl2"] = new MediaTypeHeaderValue("application/x-gl2"),
        [".gp4"] = new MediaTypeHeaderValue("application/x-gp4"),
        [".hgl"] = new MediaTypeHeaderValue("application/x-hgl"),
        [".hmr"] = new MediaTypeHeaderValue("application/x-hmr"),
        [".hpg"] = new MediaTypeHeaderValue("application/x-hpgl"),
        [".hpl"] = new MediaTypeHeaderValue("application/x-hpl"),
        [".hqx"] = new MediaTypeHeaderValue("application/mac-binhex40"),
        [".hrf"] = new MediaTypeHeaderValue("application/x-hrf"),
        [".hta"] = new MediaTypeHeaderValue("application/hta"),
        [".htc"] = new MediaTypeHeaderValue("text/x-component"),
        [".htm"] = new MediaTypeHeaderValue("text/html"),
        [".html"] = new MediaTypeHeaderValue("text/html"),
        [".htt"] = new MediaTypeHeaderValue("text/webviewhtml"),
        [".htx"] = new MediaTypeHeaderValue("text/html"),
        [".icb"] = new MediaTypeHeaderValue("application/x-icb"),
        [".ico"] = new MediaTypeHeaderValue("image/x-icon"),
        [".iff"] = new MediaTypeHeaderValue("application/x-iff"),
        [".ig4"] = new MediaTypeHeaderValue("application/x-g4"),
        [".igs"] = new MediaTypeHeaderValue("application/x-igs"),
        [".iii"] = new MediaTypeHeaderValue("application/x-iphone"),
        [".img"] = new MediaTypeHeaderValue("application/x-img"),
        [".ins"] = new MediaTypeHeaderValue("application/x-internet-signup"),
        [".isp"] = new MediaTypeHeaderValue("application/x-internet-signup"),
        [".IVF"] = new MediaTypeHeaderValue("video/x-ivf"),
        [".java"] = new MediaTypeHeaderValue("java/*"),
        [".jfif"] = new MediaTypeHeaderValue("image/jpeg"),
        [".jpe"] = new MediaTypeHeaderValue("image/jpeg"),
        [".jpeg"] = new MediaTypeHeaderValue("image/jpeg"),
        [".jpg"] = new MediaTypeHeaderValue("image/jpeg"),
        [".js"] = new MediaTypeHeaderValue("application/x-javascript"),
        [".jsp"] = new MediaTypeHeaderValue("text/html"),
        [".la1"] = new MediaTypeHeaderValue("audio/x-liquid-file"),
        [".lar"] = new MediaTypeHeaderValue("application/x-laplayer-reg"),
        [".latex"] = new MediaTypeHeaderValue("application/x-latex"),
        [".lavs"] = new MediaTypeHeaderValue("audio/x-liquid-secure"),
        [".lbm"] = new MediaTypeHeaderValue("application/x-lbm"),
        [".lmsff"] = new MediaTypeHeaderValue("audio/x-la-lms"),
        [".ls"] = new MediaTypeHeaderValue("application/x-javascript"),
        [".ltr"] = new MediaTypeHeaderValue("application/x-ltr"),
        [".m1v"] = new MediaTypeHeaderValue("video/x-mpeg"),
        [".m2v"] = new MediaTypeHeaderValue("video/x-mpeg"),
        [".m3u"] = new MediaTypeHeaderValue("audio/mpegurl"),
        [".m4e"] = new MediaTypeHeaderValue("video/mpeg4"),
        [".mac"] = new MediaTypeHeaderValue("application/x-mac"),
        [".man"] = new MediaTypeHeaderValue("application/x-troff-man"),
        [".math"] = new MediaTypeHeaderValue("text/xml"),
        [".mdb"] = new MediaTypeHeaderValue("application/msaccess"),
        [".mfp"] = new MediaTypeHeaderValue("application/x-shockwave-flash"),
        [".mht"] = new MediaTypeHeaderValue("message/rfc822"),
        [".mhtml"] = new MediaTypeHeaderValue("message/rfc822"),
        [".mi"] = new MediaTypeHeaderValue("application/x-mi"),
        [".mid"] = new MediaTypeHeaderValue("audio/mid"),
        [".midi"] = new MediaTypeHeaderValue("audio/mid"),
        [".mil"] = new MediaTypeHeaderValue("application/x-mil"),
        [".mml"] = new MediaTypeHeaderValue("text/xml"),
        [".mnd"] = new MediaTypeHeaderValue("audio/x-musicnet-download"),
        [".mns"] = new MediaTypeHeaderValue("audio/x-musicnet-stream"),
        [".mocha"] = new MediaTypeHeaderValue("application/x-javascript"),
        [".movie"] = new MediaTypeHeaderValue("video/x-sgi-movie"),
        [".mp1"] = new MediaTypeHeaderValue("audio/mp1"),
        [".mp2"] = new MediaTypeHeaderValue("audio/mp2"),
        [".mp2v"] = new MediaTypeHeaderValue("video/mpeg"),
        [".mp3"] = new MediaTypeHeaderValue("audio/mp3"),
        [".mp4"] = new MediaTypeHeaderValue("video/mpeg4"),
        [".mpa"] = new MediaTypeHeaderValue("video/x-mpg"),
        [".mpd"] = new MediaTypeHeaderValue("application/vnd.ms-project"),
        [".mpe"] = new MediaTypeHeaderValue("video/x-mpeg"),
        [".mpeg"] = new MediaTypeHeaderValue("video/mpg"),
        [".mpg"] = new MediaTypeHeaderValue("video/mpg"),
        [".mpga"] = new MediaTypeHeaderValue("audio/rn-mpeg"),
        [".mpp"] = new MediaTypeHeaderValue("application/vnd.ms-project"),
        [".mps"] = new MediaTypeHeaderValue("video/x-mpeg"),
        [".mpt"] = new MediaTypeHeaderValue("application/vnd.ms-project"),
        [".mpv"] = new MediaTypeHeaderValue("video/mpg"),
        [".mpv2"] = new MediaTypeHeaderValue("video/mpeg"),
        [".mpw"] = new MediaTypeHeaderValue("application/vnd.ms-project"),
        [".mpx"] = new MediaTypeHeaderValue("application/vnd.ms-project"),
        [".mtx"] = new MediaTypeHeaderValue("text/xml"),
        [".mxp"] = new MediaTypeHeaderValue("application/x-mmxp"),
        [".net"] = new MediaTypeHeaderValue("image/pnetvue"),
        [".nrf"] = new MediaTypeHeaderValue("application/x-nrf"),
        [".nws"] = new MediaTypeHeaderValue("message/rfc822"),
        [".odc"] = new MediaTypeHeaderValue("text/x-ms-odc"),
        [".out"] = new MediaTypeHeaderValue("application/x-out"),
        [".p10"] = new MediaTypeHeaderValue("application/pkcs10"),
        [".p12"] = new MediaTypeHeaderValue("application/x-pkcs12"),
        [".p7b"] = new MediaTypeHeaderValue("application/x-pkcs7-certificates"),
        [".p7c"] = new MediaTypeHeaderValue("application/pkcs7-mime"),
        [".p7m"] = new MediaTypeHeaderValue("application/pkcs7-mime"),
        [".p7r"] = new MediaTypeHeaderValue("application/x-pkcs7-certreqresp"),
        [".p7s"] = new MediaTypeHeaderValue("application/pkcs7-signature"),
        [".pc5"] = new MediaTypeHeaderValue("application/x-pc5"),
        [".pci"] = new MediaTypeHeaderValue("application/x-pci"),
        [".pcl"] = new MediaTypeHeaderValue("application/x-pcl"),
        [".pcx"] = new MediaTypeHeaderValue("application/x-pcx"),
        [".pdf"] = new MediaTypeHeaderValue("application/pdf"),
        [".pdx"] = new MediaTypeHeaderValue("application/vnd.adobe.pdx"),
        [".pfx"] = new MediaTypeHeaderValue("application/x-pkcs12"),
        [".pgl"] = new MediaTypeHeaderValue("application/x-pgl"),
        [".pic"] = new MediaTypeHeaderValue("application/x-pic"),
        [".pko"] = new MediaTypeHeaderValue("application/vnd.ms-pki.pko"),
        [".pl"] = new MediaTypeHeaderValue("application/x-perl"),
        [".plg"] = new MediaTypeHeaderValue("text/html"),
        [".pls"] = new MediaTypeHeaderValue("audio/scpls"),
        [".plt"] = new MediaTypeHeaderValue("application/x-plt"),
        [".png"] = new MediaTypeHeaderValue("image/png"),
        [".pot"] = new MediaTypeHeaderValue("application/vnd.ms-powerpoint"),
        [".ppa"] = new MediaTypeHeaderValue("application/vnd.ms-powerpoint"),
        [".ppm"] = new MediaTypeHeaderValue("application/x-ppm"),
        [".pps"] = new MediaTypeHeaderValue("application/vnd.ms-powerpoint"),
        [".ppt"] = new MediaTypeHeaderValue("application/vnd.ms-powerpoint"),
        [".pr"] = new MediaTypeHeaderValue("application/x-pr"),
        [".prf"] = new MediaTypeHeaderValue("application/pics-rules"),
        [".prn"] = new MediaTypeHeaderValue("application/x-prn"),
        [".prt"] = new MediaTypeHeaderValue("application/x-prt"),
        [".ps"] = new MediaTypeHeaderValue("application/postscript"),
        [".ptn"] = new MediaTypeHeaderValue("application/x-ptn"),
        [".pwz"] = new MediaTypeHeaderValue("application/vnd.ms-powerpoint"),
        [".r3t"] = new MediaTypeHeaderValue("text/vnd.rn-realtext3d"),
        [".ra"] = new MediaTypeHeaderValue("audio/vnd.rn-realaudio"),
        [".ram"] = new MediaTypeHeaderValue("audio/x-pn-realaudio"),
        [".ras"] = new MediaTypeHeaderValue("application/x-ras"),
        [".rat"] = new MediaTypeHeaderValue("application/rat-file"),
        [".rdf"] = new MediaTypeHeaderValue("text/xml"),
        [".rec"] = new MediaTypeHeaderValue("application/vnd.rn-recording"),
        [".red"] = new MediaTypeHeaderValue("application/x-red"),
        [".rgb"] = new MediaTypeHeaderValue("application/x-rgb"),
        [".rjs"] = new MediaTypeHeaderValue("application/vnd.rn-realsystem-rjs"),
        [".rjt"] = new MediaTypeHeaderValue("application/vnd.rn-realsystem-rjt"),
        [".rlc"] = new MediaTypeHeaderValue("application/x-rlc"),
        [".rle"] = new MediaTypeHeaderValue("application/x-rle"),
        [".rm"] = new MediaTypeHeaderValue("application/vnd.rn-realmedia"),
        [".rmf"] = new MediaTypeHeaderValue("application/vnd.adobe.rmf"),
        [".rmi"] = new MediaTypeHeaderValue("audio/mid"),
        [".rmj"] = new MediaTypeHeaderValue("application/vnd.rn-realsystem-rmj"),
        [".rmm"] = new MediaTypeHeaderValue("audio/x-pn-realaudio"),
        [".rmp"] = new MediaTypeHeaderValue("application/vnd.rn-rn_music_package"),
        [".rms"] = new MediaTypeHeaderValue("application/vnd.rn-realmedia-secure"),
        [".rmvb"] = new MediaTypeHeaderValue("application/vnd.rn-realmedia-vbr"),
        [".rmx"] = new MediaTypeHeaderValue("application/vnd.rn-realsystem-rmx"),
        [".rnx"] = new MediaTypeHeaderValue("application/vnd.rn-realplayer"),
        [".rp"] = new MediaTypeHeaderValue("image/vnd.rn-realpix"),
        [".rpm"] = new MediaTypeHeaderValue("audio/x-pn-realaudio-plugin"),
        [".rsml"] = new MediaTypeHeaderValue("application/vnd.rn-rsml"),
        [".rt"] = new MediaTypeHeaderValue("text/vnd.rn-realtext"),
        [".rtf"] = new MediaTypeHeaderValue("application/msword"),
        [".rv"] = new MediaTypeHeaderValue("video/vnd.rn-realvideo"),
        [".sam"] = new MediaTypeHeaderValue("application/x-sam"),
        [".sat"] = new MediaTypeHeaderValue("application/x-sat"),
        [".sdp"] = new MediaTypeHeaderValue("application/sdp"),
        [".sdw"] = new MediaTypeHeaderValue("application/x-sdw"),
        [".sit"] = new MediaTypeHeaderValue("application/x-stuffit"),
        [".slb"] = new MediaTypeHeaderValue("application/x-slb"),
        [".sld"] = new MediaTypeHeaderValue("application/x-sld"),
        [".slk"] = new MediaTypeHeaderValue("drawing/x-slk"),
        [".smi"] = new MediaTypeHeaderValue("application/smil"),
        [".smil"] = new MediaTypeHeaderValue("application/smil"),
        [".smk"] = new MediaTypeHeaderValue("application/x-smk"),
        [".snd"] = new MediaTypeHeaderValue("audio/basic"),
        [".sol"] = new MediaTypeHeaderValue("text/plain"),
        [".sor"] = new MediaTypeHeaderValue("text/plain"),
        [".spc"] = new MediaTypeHeaderValue("application/x-pkcs7-certificates"),
        [".spl"] = new MediaTypeHeaderValue("application/futuresplash"),
        [".spp"] = new MediaTypeHeaderValue("text/xml"),
        [".ssm"] = new MediaTypeHeaderValue("application/streamingmedia"),
        [".sst"] = new MediaTypeHeaderValue("application/vnd.ms-pki.certstore"),
        [".stl"] = new MediaTypeHeaderValue("application/vnd.ms-pki.stl"),
        [".stm"] = new MediaTypeHeaderValue("text/html"),
        [".sty"] = new MediaTypeHeaderValue("application/x-sty"),
        [".svg"] = new MediaTypeHeaderValue("text/xml"),
        [".swf"] = new MediaTypeHeaderValue("application/x-shockwave-flash"),
        [".tdf"] = new MediaTypeHeaderValue("application/x-tdf"),
        [".tg4"] = new MediaTypeHeaderValue("application/x-tg4"),
        [".tga"] = new MediaTypeHeaderValue("application/x-tga"),
        [".tiff"] = new MediaTypeHeaderValue("image/tiff"),
        [".tld"] = new MediaTypeHeaderValue("text/xml"),
        [".top"] = new MediaTypeHeaderValue("drawing/x-top"),
        [".torrent"] = new MediaTypeHeaderValue("application/x-bittorrent"),
        [".tsd"] = new MediaTypeHeaderValue("text/xml"),
        [".txt"] = new MediaTypeHeaderValue("text/plain"),
        [".uin"] = new MediaTypeHeaderValue("application/x-icq"),
        [".uls"] = new MediaTypeHeaderValue("text/iuls"),
        [".vcf"] = new MediaTypeHeaderValue("text/x-vcard"),
        [".vda"] = new MediaTypeHeaderValue("application/x-vda"),
        [".vdx"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vml"] = new MediaTypeHeaderValue("text/xml"),
        [".vpg"] = new MediaTypeHeaderValue("application/x-vpeg005"),
        [".vsd"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vss"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vst"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vsw"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vsx"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vtx"] = new MediaTypeHeaderValue("application/vnd.visio"),
        [".vxml"] = new MediaTypeHeaderValue("text/xml"),
        [".wav"] = new MediaTypeHeaderValue("audio/wav"),
        [".wax"] = new MediaTypeHeaderValue("audio/x-ms-wax"),
        [".wb1"] = new MediaTypeHeaderValue("application/x-wb1"),
        [".wb2"] = new MediaTypeHeaderValue("application/x-wb2"),
        [".wb3"] = new MediaTypeHeaderValue("application/x-wb3"),
        [".wbmp"] = new MediaTypeHeaderValue("image/vnd.wap.wbmp"),
        [".wiz"] = new MediaTypeHeaderValue("application/msword"),
        [".wk3"] = new MediaTypeHeaderValue("application/x-wk3"),
        [".wk4"] = new MediaTypeHeaderValue("application/x-wk4"),
        [".wkq"] = new MediaTypeHeaderValue("application/x-wkq"),
        [".wks"] = new MediaTypeHeaderValue("application/x-wks"),
        [".wm"] = new MediaTypeHeaderValue("video/x-ms-wm"),
        [".wma"] = new MediaTypeHeaderValue("audio/x-ms-wma"),
        [".wmd"] = new MediaTypeHeaderValue("application/x-ms-wmd"),
        [".wmf"] = new MediaTypeHeaderValue("application/x-wmf"),
        [".wml"] = new MediaTypeHeaderValue("text/vnd.wap.wml"),
        [".wmv"] = new MediaTypeHeaderValue("video/x-ms-wmv"),
        [".wmx"] = new MediaTypeHeaderValue("video/x-ms-wmx"),
        [".wmz"] = new MediaTypeHeaderValue("application/x-ms-wmz"),
        [".wp6"] = new MediaTypeHeaderValue("application/x-wp6"),
        [".wpd"] = new MediaTypeHeaderValue("application/x-wpd"),
        [".wpg"] = new MediaTypeHeaderValue("application/x-wpg"),
        [".wpl"] = new MediaTypeHeaderValue("application/vnd.ms-wpl"),
        [".wq1"] = new MediaTypeHeaderValue("application/x-wq1"),
        [".wr1"] = new MediaTypeHeaderValue("application/x-wr1"),
        [".wri"] = new MediaTypeHeaderValue("application/x-wri"),
        [".wrk"] = new MediaTypeHeaderValue("application/x-wrk"),
        [".ws"] = new MediaTypeHeaderValue("application/x-ws"),
        [".ws2"] = new MediaTypeHeaderValue("application/x-ws"),
        [".wsc"] = new MediaTypeHeaderValue("text/scriptlet"),
        [".wsdl"] = new MediaTypeHeaderValue("text/xml"),
        [".wvx"] = new MediaTypeHeaderValue("video/x-ms-wvx"),
        [".xdp"] = new MediaTypeHeaderValue("application/vnd.adobe.xdp"),
        [".xdr"] = new MediaTypeHeaderValue("text/xml"),
        [".xfd"] = new MediaTypeHeaderValue("application/vnd.adobe.xfd"),
        [".xfdf"] = new MediaTypeHeaderValue("application/vnd.adobe.xfdf"),
        [".xhtml"] = new MediaTypeHeaderValue("text/html"),
        [".xls"] = new MediaTypeHeaderValue("application/vnd.ms-excel"),
        [".xlw"] = new MediaTypeHeaderValue("application/x-xlw"),
        [".xml"] = new MediaTypeHeaderValue("text/xml"),
        [".xpl"] = new MediaTypeHeaderValue("audio/scpls"),
        [".xq"] = new MediaTypeHeaderValue("text/xml"),
        [".xql"] = new MediaTypeHeaderValue("text/xml"),
        [".xquery"] = new MediaTypeHeaderValue("text/xml"),
        [".xsd"] = new MediaTypeHeaderValue("text/xml"),
        [".xsl"] = new MediaTypeHeaderValue("text/xml"),
        [".xslt"] = new MediaTypeHeaderValue("text/xml"),
        [".xwd"] = new MediaTypeHeaderValue("application/x-xwd"),
        [".x_b"] = new MediaTypeHeaderValue("application/x-x_b"),
        [".sis"] = new MediaTypeHeaderValue("application/vnd.symbian.install"),
        [".sisx"] = new MediaTypeHeaderValue("application/vnd.symbian.install"),
        [".x_t"] = new MediaTypeHeaderValue("application/x-x_t"),
        [".ipa"] = new MediaTypeHeaderValue("application/vnd.iphone"),
        [".apk"] = new MediaTypeHeaderValue("application/vnd.android.package-archive"),
        [".xap"] = new MediaTypeHeaderValue("application/x-silverlight-app"),
    };

    /// <summary>
    /// 注册新的文件后缀名和Content-Type 标头
    /// </summary>
    /// <exception cref="ArgumentException"> <paramref name="extension"/> 或 <paramref name="mediaType"/> 为空</exception>
    public static void Register(string extension, string mediaType)
    {
        if (string.IsNullOrWhiteSpace(extension))
        {
            throw new ArgumentException($"“{nameof(extension)}”不能为 Null 或空白", nameof(extension));
        }

        if (string.IsNullOrWhiteSpace(mediaType))
        {
            throw new ArgumentException($"“{nameof(mediaType)}”不能为 Null 或空白", nameof(mediaType));
        }

        if (extension[0] != '.')
        {
            extension = "." + extension;
        }
        lock (_mapping)
        {
            _mapping[extension] = new MediaTypeHeaderValue(mediaType);
        }
    }

    /// <summary>
    /// 默认 Content-Type 标头 "application/octet-stream"
    /// </summary>
    public static MediaTypeHeaderValue Default => (MediaTypeHeaderValue)_default.Clone();

    /// <summary>
    /// 根据文件名获取对应的 Content-Type 标头
    /// </summary>
    public static MediaTypeHeaderValue Mapping(string fileName)
    {
        if (fileName is null || fileName.Length == 0)
        {
            return Default;
        }
        if (fileName[0] != '.')
        {
            fileName = Path.GetExtension(fileName);
            if (string.IsNullOrWhiteSpace(fileName))
            {
                fileName = "." + fileName;
            }
        }
        if (_mapping.TryGetValue(fileName, out var media))
        {
            return (MediaTypeHeaderValue)media.Clone();
        }
        return Default;
    }
}
